home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.7 / tcpdump- / tcpdump-richard-1.7 / tcpdump-3.0 / print-sl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-09  |  5.6 KB  |  244 lines

  1. /*
  2.  * Copyright (c) 1989, 1990, 1991, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #ifndef lint
  23. static  char rcsid[] =
  24.     "@(#)$Header: print-sl.c,v 1.28 94/06/10 17:01:38 mccanne Exp $ (LBL)";
  25. #endif
  26.  
  27. #ifdef CSLIP
  28. #include <sys/param.h>
  29. #include <sys/time.h>
  30. #include <sys/timeb.h>
  31. #include <sys/file.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/mbuf.h>
  34. #include <sys/socket.h>
  35.  
  36. #include <net/if.h>
  37. #include <netinet/in.h>
  38. #include <netinet/in_systm.h>
  39. #include <netinet/ip.h>
  40. #include <netinet/if_ether.h>
  41. #include <netinet/ip_var.h>
  42. #include <netinet/udp.h>
  43. #include <netinet/udp_var.h>
  44. #include <netinet/tcp.h>
  45. #include <netinet/tcpip.h>
  46.  
  47. #include <net/slcompress.h>
  48. #include <net/slip.h>
  49.  
  50. #include <ctype.h>
  51. #include <errno.h>
  52. #include <netdb.h>
  53. #include <pcap.h>
  54. #include <signal.h>
  55. #include <stdio.h>
  56.  
  57. #include "interface.h"
  58. #include "addrtoname.h"
  59.  
  60. static int lastlen[2][256];
  61. static int lastconn = 255;
  62.  
  63. static void sliplink_print(const u_char *, const struct ip *, int);
  64. static void compressed_sl_print(const u_char *, const struct ip *, int, int);
  65.  
  66. void
  67. sl_if_print(u_char *user, const struct pcap_hdr *h, const u_char *p)
  68. {
  69.     register int caplen = h->caplen;
  70.     register int length = h->len;
  71.     register const struct ip *ip;
  72.  
  73.     ts_print(&h->ts);
  74.  
  75.     if (caplen < SLIP_HDRLEN) {
  76.         printf("[|slip]");
  77.         goto out;
  78.     }
  79.     /*
  80.      * Some printers want to get back at the link level addresses,
  81.      * and/or check that they're not walking off the end of the packet.
  82.      * Rather than pass them all the way down, we set these globals.
  83.      */
  84.     packetp = p;
  85.     snapend = p + caplen;
  86.  
  87.     length -= SLIP_HDRLEN;
  88.  
  89.     ip = (struct ip *)(p + SLIP_HDRLEN);
  90.  
  91.     if (eflag)
  92.         sliplink_print(p, ip, length);
  93.  
  94.     ip_print((u_char *)ip, length);
  95.  
  96.     if (xflag)
  97.         default_print((u_char *)ip, caplen - SLIP_HDRLEN);
  98.  out:
  99.     putchar('\n');
  100. }
  101.  
  102. static void
  103. sliplink_print(register const u_char *p, register const struct ip *ip,
  104.            register int length)
  105. {
  106.     int dir;
  107.     int hlen;
  108.  
  109.     dir = p[SLX_DIR];
  110.     putchar(dir == SLIPDIR_IN ? 'I' : 'O');
  111.     putchar(' ');
  112.  
  113.     if (nflag) {
  114.         /* XXX just dump the header */
  115.         int i;
  116.  
  117.         for (i = 0; i < 15; ++i)
  118.             printf("%02x.", p[SLX_CHDR + i]);
  119.         printf("%02x: ", p[SLX_CHDR + 15]);
  120.         return;
  121.     }
  122.     switch (p[SLX_CHDR] & 0xf0) {
  123.  
  124.     case TYPE_IP:
  125.         printf("ip %d: ", length + SLIP_HDRLEN);
  126.         break;
  127.  
  128.     case TYPE_UNCOMPRESSED_TCP:
  129.         /*
  130.          * The connection id is stode in the IP protcol field.
  131.          */
  132.         lastconn = ip->ip_p;
  133.         hlen = ip->ip_hl;
  134.         hlen += ((struct tcphdr *)&((int *)ip)[hlen])->th_off;
  135.         lastlen[dir][lastconn] = length - (hlen << 2);
  136.         printf("utcp %d: ", lastconn);
  137.         break;
  138.  
  139.     default:
  140.         if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
  141.             compressed_sl_print(&p[SLX_CHDR], ip,
  142.                 length, dir);
  143.             printf(": ");
  144.         } else
  145.             printf("slip-%d!: ", p[SLX_CHDR]);
  146.     }
  147. }
  148.  
  149. static const u_char *
  150. print_sl_change(const char *str, register const u_char *cp)
  151. {
  152.     register u_int i;
  153.  
  154.     if ((i = *cp++) == 0) {
  155.         i = (cp[0] << 8) | cp[1];
  156.         cp += 2;
  157.     }
  158.     printf(" %s%d", str, i);
  159.     return (cp);
  160. }
  161.  
  162. static const u_char *
  163. print_sl_winchange(register const u_char *cp)
  164. {
  165.     register short i;
  166.  
  167.     if ((i = *cp++) == 0) {
  168.         i = (cp[0] << 8) | cp[1];
  169.         cp += 2;
  170.     }
  171.     if (i >= 0)
  172.         printf(" W+%d", i);
  173.     else
  174.         printf(" W%d", i);
  175.     return (cp);
  176. }
  177.  
  178. static void
  179. compressed_sl_print(const u_char *chdr, const struct ip *ip,
  180.             int length, int dir)
  181. {
  182.     register const u_char *cp = chdr;
  183.     register u_int flags;
  184.     int hlen;
  185.  
  186.     flags = *cp++;
  187.     if (flags & NEW_C) {
  188.         lastconn = *cp++;
  189.         printf("ctcp %d", lastconn);
  190.     } else
  191.         printf("ctcp *");
  192.  
  193.     /* skip tcp checksum */
  194.     cp += 2;
  195.  
  196.     switch (flags & SPECIALS_MASK) {
  197.     case SPECIAL_I:
  198.         printf(" *SA+%d", lastlen[dir][lastconn]);
  199.         break;
  200.  
  201.     case SPECIAL_D:
  202.         printf(" *S+%d", lastlen[dir][lastconn]);
  203.         break;
  204.  
  205.     default:
  206.         if (flags & NEW_U)
  207.             cp = print_sl_change("U=", cp);
  208.         if (flags & NEW_W)
  209.             cp = print_sl_winchange(cp);
  210.         if (flags & NEW_A)
  211.             cp = print_sl_change("A+", cp);
  212.         if (flags & NEW_S)
  213.             cp = print_sl_change("S+", cp);
  214.         break;
  215.     }
  216.     if (flags & NEW_I)
  217.         cp = print_sl_change("I+", cp);
  218.  
  219.     /*
  220.      * 'hlen' is the length of the uncompressed TCP/IP header (in words).
  221.      * 'cp - chdr' is the length of the compressed header.
  222.      * 'length - hlen' is the amount of data in the packet.
  223.      */
  224.     hlen = ip->ip_hl;
  225.     hlen += ((struct tcphdr *)&((int32 *)ip)[hlen])->th_off;
  226.     lastlen[dir][lastconn] = length - (hlen << 2);
  227.     printf(" %d (%d)", lastlen[dir][lastconn], cp - chdr);
  228. }
  229. #else
  230. #include <sys/types.h>
  231. #include <sys/time.h>
  232.  
  233. #include <stdio.h>
  234.  
  235. #include "interface.h"
  236. void
  237. sl_if_print(u_char *user, const struct pcap_hdr *h, const u_char *p)
  238. {
  239.  
  240.     error("not configured for slip");
  241.     /* NOTREACHED */
  242. }
  243. #endif
  244.